package com.cskaoyan.user.utils;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.form.UserRegisterRequest;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/25 21:48
 * @Description:
 * @PACKAGE_NAME： com.cskaoyan.user.utils
 */
public class VerifyRegisterDataUtils {
    public static void verifyRegisterData(UserRegisterRequest userRegisterRequest){
        String email = userRegisterRequest.getEmail();
        String userName = userRegisterRequest.getUserName();
        String userPwd = userRegisterRequest.getUserPwd();

        //邮箱re校验
        if (email==null || !email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){
            throw new BizException(UserRetCode.REGISTER_EMAIL_VERIFY_ERR.getCode(),
                    UserRetCode.REGISTER_EMAIL_VERIFY_ERR.getMessage());
        }
        //密码必须6-16位,包含字母和数字的组合,不能使用特殊字符
        if (userPwd==null || ! userPwd.matches("^(?=.*\\d)(?=.*[a-zA-Z]).{6,16}$")){
            throw new BizException(UserRetCode.REGISTER_PASS_VERIFY_ERR.getCode(),
                    UserRetCode.REGISTER_PASS_VERIFY_ERR.getMessage());
        }
        //用户名校验(字母开头，允许5-18字节，允许字母数字下划线)
        if (userName==null|| !userName.matches("^[a-zA-Z][a-zA-Z0-9_]{5,18}$") ){
            throw new BizException(UserRetCode.REGISTER_USERNAME_VERIFY_ERR.getCode(),
                    UserRetCode.REGISTER_USERNAME_VERIFY_ERR.getMessage());
        }
    }
}
